package cn.tedu.micro.demo.device.service;

import cn.tedu.micro.demo.common.pojo.po.GunInfoPO;
import cn.tedu.micro.demo.common.pojo.vo.GunInfoVO;
import cn.tedu.micro.demo.device.mapper.GunInfoMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DeviceServiceImpl implements DeviceService {
    @Autowired
    private GunInfoMapper gunInfoMapper;
    @Override
    public GunInfoVO getGunStatus(Integer gunId) {
        //调用持久层 读取gunId对应的gunInfo
        //调用持久层 使用gunId参数 查询gunInfoPO
        //select * from gun_info where id=#{gunId}
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id",gunId);
        gunInfoMapper.selectOne(queryWrapper);
//        GunInfoPO gunInfoPO = gunInfoMapper.selectById(gunId);
        GunInfoPO gunInfoPO = gunInfoMapper.selectOne(queryWrapper);
        //拷贝bean
        GunInfoVO vo=new GunInfoVO();
        BeanUtils.copyProperties(gunInfoPO,vo);//po是空,方法空指针异常
        return vo;
    }
    //select * from gun_info where id=?
    //案例1 C
    //insert into gun_info (id,status,type,create_time,update_time)
    //values (#{id},#{status},#{type},#{createTime},#{updateTime})
        /*GunInfoPO poParam=  new GunInfoPO();
        //业务数据写入poParam
        gunInfoMapper.insert(poParam);*/
    //案例2 D
    //delete from gun_info where
    //条件 eq(=) where id=#{id} || where type=#{type} || where id=#{id} and type=#{type}
    //条件 gt(>) lt(<) ge(>=) le(<=) where create_time<#{start}
    //条件 in(in) where id in (#{id1},#{id2})
    //条件 like(like) where name like concat (%,#{name},%)
    //QueryWrapper queryWrapper = new QueryWrapper();
    //给sql语句拼接的时候提供where条件的
    //queryWrapper.eq("id",1);
    //queryWrapper.eq("name","王");//where id=1 and name=王
    //queryWrapper.eq("id", 1);
    //queryWrapper.or();
    //queryWrapper.eq("name","王");//where id=1 or name=王
    //where id in (1,2,3,4,5)
    //List<Long> ids= List.of(1l,2l,3l,4l,5l);
    //queryWrapper.in("id",ids);
    //gunInfoMapper.delete(queryWrapper);
    //案例3 select
    //select id,status,type,create_time,update_time
    //from gun_info where id in (1,2,3,4,5)
        /*QueryWrapper queryWrapper = new QueryWrapper();
        List<Long> ids= List.of(1l,2l,3l,4l,5l);
        queryWrapper.in("id",ids);
        List list = gunInfoMapper.selectList(queryWrapper);//gunInfoPo集合
        //select id,status,type,create_time,update_time
        //from gun_info where id = 1
        queryWrapper.eq("id",1);
        gunInfoMapper.selectOne(queryWrapper);*/
    //案例4 U
    //update gun_info set type=5 where id=1 and status=1
        /*UpdateWrapper updateWrapper = new UpdateWrapper();
        //包装set数据
        //包装where条件 相关api和queryWrapper完全一样
        updateWrapper.setSql("type=5");
        //包装条件where
        updateWrapper.eq("id",1);
        updateWrapper.eq("status",1);
        gunInfoMapper.update(updateWrapper);*/
}
